﻿using ITSM.Model.Entity;
using ITSM.Service;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ITSM.Controllers.ContingencyPlanTable
{
    [Route("api/[controller]")]
    [ApiController]
    public class ContingencyPlanTableController : ControllerBase
    {
        private readonly IContingencyPlanTable _planTable;
        public ContingencyPlanTableController(IContingencyPlanTable planTable)
        {
            _planTable = planTable;
        }

        // POST api/<GetContingencyPlanTable>
        [HttpGet("/api/GetContingencyPlanTable")]
        public IActionResult GetContingencyPlanTable(string title)
        {
            return Ok(_planTable.QueryContingencyPlanTable(title));
        }

        // POST api/<GetContingencyPlanTableHistory>
        [HttpGet("/api/GetContingencyPlanTableHistory")]
        public IActionResult GetContingencyPlanTableHistory(string title)
        {
            return Ok(_planTable.QueryContingencyPlanTableHistory(title));
        }

        // POST api/<ContingencyPlanTableController>
        [HttpGet("/api/GetContingencyPlanTableInfo")]
        public IActionResult GetContingencyPlanTableInfo(string ContingencyPlanNo)
        {
            if (string.IsNullOrWhiteSpace(ContingencyPlanNo))
            {
                return NotFound();
            }
            try
            {
                return Ok(_planTable.QueryContingencyPlanTableInfo(Convert.ToInt32(ContingencyPlanNo)));
            }
            catch (Exception)
            {
                return NotFound();
            }
        }

        // POST api/<ContingencyPlanTableController>
        [HttpGet("/api/GetContingencyPlanTableHistoryInfo")]
        public IActionResult GetContingencyPlanTableHistoryInfo(string HistoryId)
        {
            if (string.IsNullOrWhiteSpace(HistoryId))
            {
                return NotFound();
            }
            try
            {
                return Ok(_planTable.QueryContingencyPlanTableHistoryInfo(HistoryId));
            }
            catch (Exception)
            {
                return NotFound();
            }
        }

        // POST api/<GetContingencyPlanTableInfoIng>
        [HttpGet("/api/GetContingencyPlanTableInfoIng")]
        public IActionResult GetContingencyPlanTableInfoIng(string ContingencyPlanId)
        {
            if (string.IsNullOrWhiteSpace(ContingencyPlanId))
            {
                return NotFound();
            }
            try
            {
                return Ok(_planTable.QueryContingencyPlanTableInfoIng(ContingencyPlanId));
            }
            catch (Exception)
            {
                return NotFound();
            }
        }

        // POST api/<AddContingencyPlanTable>
        [HttpPost("/api/AddContingencyPlanTable")]
        public IActionResult AddContingencyPlanTable(RYD_ContingencyPlanTable planTable)
        {
            if (string.IsNullOrWhiteSpace(planTable.RYD_ContingencyPlanTitle) || string.IsNullOrWhiteSpace(planTable.RYD_ContingencyPlanExplain) ||
                string.IsNullOrWhiteSpace(planTable.RYD_ContingencyPlanDetail))
            {
                return NotFound();
            }
            if (_planTable.AddContingencyPlanTable(planTable))
            {
                return Ok();
            }
            return NotFound();
        }

        // POST api/<AddContingencyPlanTable>
        [HttpPost("/api/PublishContingencyPlanTable")]
        public IActionResult PublishContingencyPlanTable(RYD_ContingencyPlanTableDetail planTable)
        {
            if (string.IsNullOrWhiteSpace(planTable.RYD_ContingencyPlanTitle) || string.IsNullOrWhiteSpace(planTable.RYD_ContingencyPlanExplain) ||
                string.IsNullOrWhiteSpace(planTable.RYD_ContingencyPlanDetail))
            {
                return NotFound();
            }
            if (_planTable.PublishContingencyPlanTable(planTable))
            {
                return Ok();
            }
            return NotFound();
        }
    }
}
